package sq.sms;


import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 短信发送接口配置
 * @Author sunquan
 */
@Configuration
public class SmsConfiguration {
    private static Logger logger = LoggerFactory.getLogger(SmsConfiguration.class);
    private static final String CN_HANGZHOU = "cn-hangzhou";
    @Value("${product}")
    public String product;
    @Value("${domain}")
    public String domain;
    @Value("${accessKeyId}")
    public String accessKeyId;
    @Value("${accessKeySecret}")
    public String accessKeySecret;

    @Bean(name = "iAcsClient")
    public IAcsClient provideIAcsClient() {
        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile(CN_HANGZHOU, accessKeyId, accessKeySecret);
        try {
            DefaultProfile.addEndpoint(CN_HANGZHOU, CN_HANGZHOU, product, domain);
        } catch (ClientException e) {
            logger.error("1.客户端丢失.如处在无人区;2.阿里服务器抛出异常,导致客户端未接收到短信;3.手机号无效,无法接收短信", e);
        }
        return new DefaultAcsClient(profile);
    }


    @Bean
    public TplSmsSender provideSSender(IAcsClient client) {
        return new SqSmsSender(client);
    }
}
